;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun maximize-frame-horz ()
  (interactive)
  (x-send-client-message
   nil 0 nil "_NET_WM_STATE" 32
   '(1 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)))

(defun maximize-frame-vert ()
  (interactive)
  (x-send-client-message
   nil 0 nil "_NET_WM_STATE" 32
   '(1 "_NET_WM_STATE_MAXIMIZED_VERT" 0)))

(defun maximize-frame ()
  (interactive)
  (maximize-frame-horz)
  (maximize-frame-vert))

(defun match-paren (arg)
  "Go to the matching paren if on a paren; otherwise insert %."
  (interactive "p")
  (cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
        ((looking-at "\\s\)") (forward-char 1) (backward-list 1))
        (t (self-insert-command (or arg 1)))))

;; (defun my-indent-or-complete ()
;;   (interactive)
;;   (if (looking-at "\\>")
;;       (hippie-expand nil)
;;     (indent-for-tab-command)))

(defun electric-pair ()
  "Insert character pair without sournding spaces"
  (interactive)
  (let (parens-require-spaces)
    (insert-pair)))

(defun open-next-line (arg)
 "Move to the next line (like vi) and then opens a line."
 (interactive "p")
 (end-of-line)
 (open-line arg)
 (next-line 1)
 (indent-according-to-mode))

(defun indent-all ()
  "Indent entire buffer"
  (interactive)
  (indent-region (point-min) (point-max) nil))

(defun cc-return ()
  (interactive)
  (c-indent-line)
  (newline-and-indent))

(defun move-line (&optional n)
  "Move current line N (1) lines up/down leaving point in place."
  (interactive "p")
  (when (null n)
    (setq n 1))
  (let ((col (current-column)))
    (beginning-of-line)
    (next-line 1)
    (transpose-lines n)
    (previous-line 1)
    (forward-char col)))

(defun move-line-up (n)
  "Moves current line N (1) lines up leaving point in place."
  (interactive "p")
  (move-line (if (null n) -1 (- n))))

(defun move-line-down (n)
  "Moves current line N (1) lines down leaving point in place."
  (interactive "p")
  (move-line (if (null n) 1 n)))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; settings
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(setq inhibit-startup-message t)

;(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(transient-mark-mode t)

(setq kill-whole-line t) ; Killing line also deletes \n

(setq kill-ring-max 50000)
(setq default-fill-column 79)
(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)
(show-paren-mode t) ;show matching parenthesis
(setq show-paren-style 'parenteses)
(setq query-replace-highlight t) ;highlight during searching
(setq search-highlight t) ; highlight incremental search
(setq mouse-yank-at-point t)
(mouse-avoidance-mode 'animate)
(setq-default scroll-step 1)
(setq-default indent-tabs-mode nil)
(setq c-tab-always-indent nil)

(setq column-number-mode t)
(setq display-time-24hr-format t)
(display-time)

(defalias 'yes-or-no-p 'y-or-n-p)

(setq visible-bell t)  ;flash the frame to represent a bell

(global-auto-revert-mode t)

(setq dired-recursive-copies t)
(setq dired-recursive-deletes t)

;; (setq make-backup-files nil)
(setq backup-directory-alist
      (quote (("." . "~/.emacs-backups/"))))
(setq version-control t)
(setq kept-old-versions 2)
(setq kept-new-versions 2)
(setq delete-old-versions t)
(setq backup-by-copying t)

(setq x-select-enable-clipboard t)

(setq require-final-newline t)

(server-start)
(add-hook 'server-switch-hook 
          (lambda ()
            (local-set-key (kbd "C-x k") 'server-edit)))

(setq default-major-mode 'text-mode)
(add-hook 'text-mode-hook
          (lambda ()
            (turn-on-auto-fill)))


;; (set-default-font "Crisp-12")
(set-default-font "Courier 10 Pitch-11")
;; (set-fontset-font (frame-parameter nil 'font)
;;      'unicode '("WenQuanYi Bitmap Song" . "unicode-bmp") 'nil 'append) 
(set-fontset-font "fontset-default"
                  'unicode '("Microsoft YaHei". "unicode-bmp"))



(setq user-full-name "Angus He")
(setq user-mail-address "angushe@gmail.com")

(set-language-environment "UTF-8")
;; (set-language-environment 'Chinese-GB)
(prefer-coding-system 'mule-utf-8)
(setq locale-coding-system 'mule-utf-8)
(set-terminal-coding-system 'mule-utf-8)
(set-keyboard-coding-system 'mule-utf-8)
(set-selection-coding-system 'mule-utf-8)

(global-hl-line-mode t)
(set-face-background 'hl-line "#111")

(setq compile-command "make")

(maximize-frame)
